import { Body, Controller, Post, Get, Req } from '@nestjs/common';
import { BaseColorService } from './color.service';
import { ADMIN_PREFIX } from '../../admin.constants';
import {
  ApiOkResponse,
  ApiOperation,
  ApiSecurity,
  ApiTags,
} from '@nestjs/swagger';
import { AdminUser } from '../../core/decorators/admin-user.decorator';
import { FastifyRequest } from 'fastify';

@ApiSecurity(ADMIN_PREFIX)
@ApiTags('水墨管理模块')
@Controller('color')
export class BaseColorController {
  constructor(private baseColorService: BaseColorService) { }

  @ApiOperation({ summary: '获取水墨列表' })
  @ApiOkResponse()
  @Get('list')
  async list() {
    return await this.baseColorService.list();
  }

  @ApiOperation({ summary: '分页获取水墨列表' })
  @ApiOkResponse()
  @Post('page')
  async page(@Body() dto) {
    const [list, total] = await this.baseColorService.page(dto);

    return {
      list,
      pagination: {
        total,
        pageNum: dto.pageNum,
        pageSize: dto.pageSize,
      },
    };
  }

  @ApiOperation({ summary: '添加水墨' })
  @ApiOkResponse()
  @Post('add')
  async add(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.baseColorService.isExistKey(dto);
    await this.baseColorService.add(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '更新水墨' })
  @ApiOkResponse()
  @Post('update')
  async update(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.baseColorService.update(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '删除水墨' })
  @ApiOkResponse()
  @Post('delete')
  async delete(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.baseColorService.delete(dto, user, mac, ip, syncHostSqlite);
  }
}
